package com.chebaohui.iqs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.*;
import com.chebaohui.iqs.entity.base.*;
import com.chebaohui.iqs.entity.req.*;
import com.chebaohui.iqs.entity.res.*;
import com.chebaohui.iqs.service.PriceService;
import com.chebaohui.iqs.sign.SignParam;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * describe:控制层:车险报价
 * 
 * @author regas_xie
 *
 */
@Api(value = "price", description = "车险算费接口", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping(value = "price")
public class PriceController {
	/**
	 * log:日志输出
	 */
	private static Log log = LogFactory.getLog(PriceController.class);
	
	@Autowired
	private PriceService priceService;

	@ApiOperation(value = "车辆查询", httpMethod = "POST", notes = "车险报价第一步:查车")
	@RequestMapping(value = "/checkCar", method = RequestMethod.POST)
	public Result<List<CompanyCars<List<Cars>>>> checkCar(
			@ApiParam(value = "车辆查询请求参数", required = true) @RequestBody(required = true) Paramter<CheckCar> param) {
		Result<List<CompanyCars<List<Cars>>>> result=null;
		
		if (!SignParam.decrypted(param.getSign()))
			result= new Result<>(ResultCode.L01);
		else
			result=priceService.carCheck(param);
		
		log.info("车辆查询:"
		+"\n"+"请求参数："+JSONObject.toJSONString(param)
		+"\n"+"返回结果："+JSONObject.toJSONString(result));
		return result;
	}

	@ApiOperation(value = "车险报价", httpMethod = "POST", notes = "车险报价第二步:报价")
	@RequestMapping(value = "/carPrice", method = RequestMethod.POST)
	public Result<List<CompanyCarPrice>> carPrice(
			@ApiParam(value = "车辆报价请求参数", required = true) @RequestBody(required = true) Paramter<CarPrice> param) {
		Result<List<CompanyCarPrice>> result=null;
		
		if (!SignParam.decrypted(param.getSign()))
			result= new Result<>(ResultCode.L01);
		else
			result=priceService.carPrice(param);
		
		log.info("车险报价:"+""
				+"\n"+"请求参数："+JSONObject.toJSONString(param)
				+"\n"+"返回结果："+JSONObject.toJSONString(result));
		return result;
	}

	@ApiOperation(value = "车险核保", httpMethod = "POST", notes = "车险报价第三步:核保")
	@RequestMapping(value = "/confirm", method = RequestMethod.POST)
	public Result<CompanyCarPermit> carConfirm(
			@ApiParam(value = "车辆核保请求参数", required = true) @RequestBody(required = true) Paramter<CarPermit> param) {
		Result<CompanyCarPermit> result;
		
		if (!SignParam.decrypted(param.getSign()))
			result= new Result<>(ResultCode.L01);
		else
			result=priceService.carConfirm(param);
		
		log.info("车险核保:"
				+"\n"+"请求参数："+JSONObject.toJSONString(param)
				+"\n"+"返回结果："+JSONObject.toJSONString(result));
		return result;
	}

	@ApiOperation(value = "支付登记", httpMethod = "POST", notes = "车险报价第四步:支付登记")
	@RequestMapping(value = "/pay", method = RequestMethod.POST)
	public Result<CompanyCarPermit> carPay(
			@ApiParam(value = "车辆核保请求参数", required = true) @RequestBody(required = true) Paramter<CarPays> param) {
		Result<CompanyCarPermit> result=null;
		
		if (!SignParam.decrypted(param.getSign()))
			result= new Result<>(ResultCode.L01);
		else
			result=priceService.carPay(param);
		
		log.info("支付登记:"
				+"\n"+"请求参数："+JSONObject.toJSONString(param)
				+"\n"+"返回结果："+JSONObject.toJSONString(result));
		return result;
	}
	
	@ApiOperation(value = "利宝配送回调", httpMethod = "POST", notes = "利宝配送回调",hidden=true)
	@RequestMapping(value = "/libao/delivery", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void backLibao(HttpServletRequest request, HttpServletResponse response){
		log.info("利宝配送回调,请求参数:"+JSONObject.toJSONString(request.getParameterMap()));
		priceService.backDeal(request, response,"libao");
	}
	
	@ApiOperation(value = "利宝支付", httpMethod = "GET", notes = "利宝支付", hidden = true)
	@RequestMapping(value = "/libao/val", method = RequestMethod.GET)
	public ModelAndView libaoVal(
			@ApiParam(value = "订单号", required = true) @RequestParam(value = "libaoFlowId", required = true) String libaoFlowId) {
		return priceService.libaoPay(libaoFlowId);
	}
	
	@ApiOperation(value = "利宝配送回调", httpMethod = "POST", notes = "利宝配送回调",hidden=true)
	@RequestMapping(value = "/libao/pay", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void backLibaoPay(HttpServletRequest request, HttpServletResponse response){
		log.info("利宝配送回调,请求参数:"+JSONObject.toJSONString(request.getParameterMap()));
//		priceService.backDeal(request, response,"libao");
	}
	
	@ApiOperation(value = "利宝支付", httpMethod = "GET", notes = "利宝支付", hidden = true)
	@RequestMapping(value = "/libao/back", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView libaoValBack(HttpServletRequest request, HttpServletResponse response) {
		return null;
	}
}
